package com.kinghunt.misp.service.impl.custx;

import com.bygoodtech.kings.base.utils.ConvertUtil;
import com.bygoodtech.kings.common.dto.ApiResult;
import com.bygoodtech.kings.common.dto.page.PageInfo;
import com.kinghunt.misp.common.dto.custx.CustDTO;
import com.kinghunt.misp.common.param.custx.CustParam;
import com.kinghunt.misp.dal.dao.custx.CustDAO;
import com.kinghunt.misp.dal.model.custx.CustDO;
import com.kinghunt.misp.service.api.custx.CustService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 客户管理
 *
 * @Author ciaran
 * @Date 2023/7/13 21:58l
 */
@Slf4j
@Service
@Transactional
public class CustServiceImpl implements CustService {

    @Autowired(required = false)
    private CustDAO custDAO;

    @Override
    public ApiResult<PageInfo<CustDTO>> getCustList(CustParam custParam) {
        int offset = (custParam.getPageNum() - 1) * custParam.getPageSize();
        CustDO custDO = ConvertUtil.convert(custParam, CustDO.class);  // 将custParam的数据类型转为CustDO
        int count = custDAO.selectCustListCount(custDO);
        PageInfo<CustDTO> pageInfo = new PageInfo<>();
        pageInfo.setTotal(count);
        pageInfo.setPageNum(custParam.getPageNum());
        pageInfo.setPageSize(custParam.getPageSize());
        List<CustDO> custDOList = custDAO.selectCustList(custDO, offset, custParam.getPageSize());
        List<CustDTO> dto =  ConvertUtil.convert(custDOList, CustDTO.class);
        pageInfo.setData(dto);
        return ApiResult.ok(pageInfo);
    }

    @Override
    public ApiResult<CustDTO> getCustDetail(Long custId) {
        CustDO custDO = custDAO.selectByPrimaryKey(custId);
        CustDTO custDTO = ConvertUtil.convert(custDO, CustDTO.class);
        return ApiResult.ok(custDTO);
    }

    @Override
    public ApiResult addCust(CustParam custParam) {
        //判断客户号是否唯一
        List<CustDO> custDOList = custDAO.selectByCustNo(custParam.getCustNo());
        if(custDOList != null && custDOList.size() > 0){
            return ApiResult.failOfMessage("您新增的客户编号已存在！ ");
        }
        CustDO custDO = ConvertUtil.convert(custParam, CustDO.class);
        // 保存地址到数据库
        custDAO.insert(custDO);
        return ApiResult.ok();
    }

    @Override
    public ApiResult updateCust(CustParam custParam) {
        CustDO custDO = custDAO.selectByPrimaryKey(custParam.getCustId());
        if(null == custDO){
            return ApiResult.failOfMessage("您修改的客户不存在！ ");
        }
        // 修改当前地址
        CustDO updateCustDO = ConvertUtil.convert(custParam, CustDO.class);
        custDAO.updateByPrimaryKeySelective(updateCustDO);
        return ApiResult.ok();
    }

    @Override
    public ApiResult<Boolean> deleteCust(Long custId) {
        CustDO custDO = custDAO.selectByPrimaryKey(custId);
        if(null == custDO){
            return ApiResult.failOfMessage("您删除的客户不存在！ ");
        }
        custDAO.deleteByPrimaryKey(custId);
        return ApiResult.ok();
    }

    @Override
    public ApiResult<List<CustDTO>> getCustByIds(List<Long> custIds) {
        if (CollectionUtils.isEmpty(custIds)) {
            log.warn("getCustByIds 入参为空");
            return ApiResult.failOfMessage("您查找的供应商参数不正确！ ");
        }
        List<CustDO> custDOList = custDAO.selectByIds(custIds);
        List<CustDTO> dto =  ConvertUtil.convert(custDOList, CustDTO.class);
        return ApiResult.ok(dto);
    }
}
